import 'package:inmotion_pro/pages/models/firmware_info_model.dart';
import 'package:inmotion_pro/pages/models/user_model.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:safemap/safemap.dart';

// 车辆信息详情响应模型
class VehicleInfoDetailRspModel extends BaseData {
  // 设备唯一标识
  String sn;

  // 蓝牙名称
  String bluetoothName;

  // 型号名称
  String modelName;

  // 型号编码
  String modelCode;

  // 版本信息列表（每项为 Map: {device, blVersion, hwVersion, appVersion}）
  List<FirmwareInfoModel> versions;

  // 设备状态
  int state;

  // 激活时间（时间戳，毫秒）
  int activatedAt;

  // 格式化激活时间
  String get activatedAtFormatted {
    if (activatedAt == 0) {
      return "";
    }
    final dateTime = DateTime.fromMillisecondsSinceEpoch(activatedAt);
    return "${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')}";
  }

  // 更新时间（时间戳，毫秒）
  int updatedAt;

  // 总里程
  int totalMileage;

  // 蓝牙 MAC
  String btMac;

  // 蓝牙广播协议版本
  int btAdvProtocolVersion;

  // 昵称
  String nickname;

  // 拥有者列表（每项为 Map: {user_id, name, avatar, type}）
  List<UserModel> owners;

  VehicleInfoDetailRspModel({
    this.sn = "",
    this.bluetoothName = "",
    this.modelName = "",
    this.modelCode = "",
    this.versions = const [],
    this.state = 0,
    this.activatedAt = 0,
    this.updatedAt = 0,
    this.totalMileage = 0,
    this.btMac = "",
    this.btAdvProtocolVersion = 0,
    this.nickname = "",
    this.owners = const [],
  });

  factory VehicleInfoDetailRspModel.fromJson(Map<String, dynamic> json) {
    final SafeMap safeMap = SafeMap(json);
    return VehicleInfoDetailRspModel(
      sn: safeMap['sn'].string ?? "",
      bluetoothName: safeMap['device_name'].string ?? "",
      modelName: safeMap['model_name'].string ?? "",
      modelCode: safeMap['model_code'].string ?? "",
      versions: (safeMap['version'].list ?? [])
          .map(
              (item) => FirmwareInfoModel.fromMap(item as Map<String, dynamic>))
          .toList(),
      state: safeMap['state'].toInt ?? 0,
      activatedAt: safeMap['activated_at'].toInt ?? 0,
      updatedAt: safeMap['updated_at'].toInt ?? 0,
      totalMileage: safeMap['total_mileage'].toInt ?? 0,
      btMac: safeMap['bt_mac'].string ?? "",
      btAdvProtocolVersion: safeMap['bt_adv_protocol_version'].toInt ?? 0,
      nickname: safeMap['nickname'].string ?? "",
      owners: (safeMap['owners'].list ?? [])
          .map((item) => UserModel.fromMap(item))
          .toList(),
    );
  }

  toMap() {
    return {
      'sn': sn,
      'device_name': bluetoothName,
      'model_name': modelName,
      'model_code': modelCode,
      'version': versions.map((v) => v.toMap()).toList(),
      'state': state,
      'activated_at': activatedAt,
      'updated_at': updatedAt,
      'total_mileage': totalMileage,
      'bt_mac': btMac,
      'bt_adv_protocol_version': btAdvProtocolVersion,
      'nickname': nickname,
      'owners': owners.map((v) => v.toMap()).toList(),
    };
  }
}
